using UnityEngine;
using UnityEngine.AI;
using UnityEngine.Networking;

[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(CharacterSystem))]
public class AICharacterShooterNav : NetworkBehaviour
{
	[Header("Properties")]
	public string[] TargetTag = new string[1]
	{
		"Player"
	};

	public bool LockTarget = true;

	public float AIUpdateRate = 8f;

	public float SyncDistance = 10f;

	[Range(0f, 1f)]
	public float Accuracy = 0.5f;

	public float CombatTime = 10f;

	public float IdleTime = 30f;

	public GameObject ObjectTarget;

	[HideInInspector]
	public Vector3 PositionTarget;

	[HideInInspector]
	public Vector3 PositionMove;

	[HideInInspector]
	public CharacterSystem character;

	public float DistanceAttack = 300f;

	public float DistanceKill = 10f;

	[Header("Attacking")]
	public float DistanceMoveTo = 20f;

	public float TurnSpeed = 10f;

	public bool Fighting = true;

	public bool RushMode;

	public float PatrolRange = 10f;

	[HideInInspector]
	public Vector3 positionTemp;

	[HideInInspector]
	public float aiTime;

	[HideInInspector]
	public int aiState;

	private float attackTemp;

	public float AttackDelay = 0.5f;

	public float SprayTimeMax = 10f;

	public float TimeLoseTarget = 10f;

	[Header("Sound")]
	public float IdleSoundDelay = 10f;

	private float soundTime;

	private float soundTimeDuration;

	private AIManager AImange;

	private NavMeshAgent navAgent;

	private Vector3 targetDirection;

	private float timeTmp;

	public Transform Pointer;

	private float sprayTime;

	private bool isShooting;

	private float timeSprayTmp;

	private float currentDistanceWeapon;

	private float timeTmpLoseTarget;

	private Vector3 positionMoveTmp;

	private void Start()
	{
		character = base.gameObject.GetComponent<CharacterSystem>();
		navAgent = base.gameObject.GetComponent<NavMeshAgent>();
		navAgent.avoidancePriority = Random.Range(0, 100);
		positionTemp = base.transform.position;
		aiState = 0;
		attackTemp = Time.time;
		soundTime = Time.time;
		soundTimeDuration = Random.Range(0f, IdleSoundDelay);
		character.NetworkNetID = -1;
		character.isServerControl = true;
	}

	public void Attack()
	{
		if (!isShooting)
		{
			sprayTime = Random.Range(1f, SprayTimeMax);
			timeSprayTmp = Time.time;
			isShooting = true;
		}
	}

	private void attacking()
	{
		if (!isShooting)
		{
			return;
		}
		if (Time.time > attackTemp + AttackDelay)
		{
			if (Time.time < timeSprayTmp + sprayTime)
			{
				character.inventory.FPSEquipment.Trigger();
			}
			else
			{
				attackTemp = Time.time;
				isShooting = false;
			}
		}
		character.inventory.FPSEquipment.OnTriggerRelease();
	}

	private bool canSeeTarget(Vector3 pointer, GameObject target)
	{
		Vector3 normalized = (target.transform.position + Vector3.up * Random.Range(0, 100) / 100f - pointer).normalized;
		RaycastHit hitInfo;
		if (Physics.Raycast(pointer, normalized, out hitInfo) && hitInfo.collider.transform.root != base.gameObject.transform.root && hitInfo.collider.transform.root == target.transform.root)
		{
			UnityEngine.Debug.DrawLine(pointer, target.transform.position, Color.red);
			return true;
		}
		return false;
	}

	private Vector3 getRandomGo()
	{
		if ((bool)UnitZ.NetworkGameplay)
		{
			Vector3 vector = positionTemp + new Vector3(Random.Range(0f - PatrolRange, PatrolRange), 0f, Random.Range(0f - PatrolRange, PatrolRange));
			BattleRoyaleGamePlay component = UnitZ.NetworkGameplay.GetComponent<BattleRoyaleGamePlay>();
			if (component != null)
			{
				Vector2 vector2 = Random.insideUnitCircle * component.Radius;
				vector = component.CentreArea + new Vector3(vector2.x, 0f, vector2.y);
				if (Physics.Raycast(vector + Vector3.up * 100f, -Vector3.up, out RaycastHit hitInfo))
				{
					vector = hitInfo.point;
				}
			}
			positionMoveTmp = vector;
			return vector;
		}
		return positionMoveTmp = positionTemp + new Vector3(Random.Range(0f - PatrolRange, PatrolRange), 0f, Random.Range(0f - PatrolRange, PatrolRange));
	}

	private GameObject findTarget()
	{
		GameObject result = null;
		float num = float.MaxValue;
		for (int i = 0; i < TargetTag.Length; i++)
		{
			TargetCollector targetCollector = UnitZ.aiManager.FindTargetTag(TargetTag[i]);
			if (targetCollector == null)
			{
				continue;
			}
			GameObject[] targets = targetCollector.Targets;
			if (targets == null || targets.Length <= 0)
			{
				continue;
			}
			for (int j = 0; j < targets.Length; j++)
			{
				if (!(targets[j] != null) || !(targets[j].transform.root != base.transform.root))
				{
					continue;
				}
				DamageManager component = targets[j].GetComponent<DamageManager>();
				if (component != null && component.IsAlive)
				{
					float num2 = Vector3.Distance(targets[j].gameObject.transform.position, base.gameObject.transform.position);
					if (num2 <= num && (num2 <= DistanceMoveTo || num2 <= currentDistanceWeapon) && (canSeeTarget(Pointer.transform.position, targets[j].gameObject) || RushMode))
					{
						num = num2;
						result = targets[j].gameObject;
					}
				}
			}
		}
		return result;
	}

	private void Update()
	{
		if (character == null)
		{
			return;
		}
		if (Time.time > soundTime + soundTimeDuration)
		{
			character.PlayIdleSound();
			soundTimeDuration = Random.Range(0f, IdleSoundDelay);
			soundTime = Time.time;
		}
		if (!base.isServer || !character.IsAlive)
		{
			return;
		}
		float num = 1f / Time.deltaTime;
		float num2 = num / AIUpdateRate * Time.deltaTime;
		currentDistanceWeapon = character.PrimaryWeaponDistance;
		if (currentDistanceWeapon > DistanceAttack)
		{
			currentDistanceWeapon = DistanceAttack;
		}
		if (Time.time > timeTmp + num2)
		{
			timeTmp = Time.time;
			float num3 = Vector3.Distance(PositionTarget, base.gameObject.transform.position);
			if (ObjectTarget != null)
			{
				DamageManager component = ObjectTarget.GetComponent<DamageManager>();
				PositionTarget = ObjectTarget.transform.position;
				UnityEngine.Debug.DrawLine(base.transform.position, PositionTarget, Color.blue);
				Vector3 b = new Vector3((float)Random.Range(-100, 100) / 100f, (float)Random.Range(-100, 100) / 100f, (float)Random.Range(-100, 100) / 100f) * (Accuracy - 1f);
				if (Pointer != null)
				{
					Pointer.forward = (PositionTarget + b + Vector3.up * Random.Range(0, 100) / 100f - Pointer.position).normalized;
				}
				if (aiTime <= 0f)
				{
					aiState = Random.Range(0, 4);
					aiTime = Random.Range(1f, CombatTime);
					if (num3 < DistanceKill)
					{
						aiState = Random.Range(0, 2);
					}
					if (aiState == 0)
					{
						PositionMove = base.transform.position;
					}
					if (aiState == 1 || aiState == 2)
					{
						PositionMove = getRandomGo();
					}
				}
				else
				{
					aiTime -= 1f / AIUpdateRate;
				}
				if (num3 <= currentDistanceWeapon)
				{
					if (canSeeTarget(Pointer.transform.position, ObjectTarget))
					{
						if (aiState == 0)
						{
							Attack();
							PositionMove = base.transform.position;
							targetDirection = PositionTarget - base.transform.position;
						}
						if (aiState == 1)
						{
							Attack();
							PositionMove = positionMoveTmp;
							targetDirection = PositionTarget - base.transform.position;
						}
						if (aiState == 2)
						{
							isShooting = false;
							PositionMove = positionMoveTmp;
							targetDirection = navAgent.desiredVelocity.normalized;
						}
						timeTmpLoseTarget = Time.time;
					}
					else
					{
						if (aiState != 2)
						{
							PositionMove = PositionTarget;
						}
						targetDirection = navAgent.desiredVelocity.normalized;
						isShooting = false;
						if (Time.time >= timeTmpLoseTarget + TimeLoseTarget)
						{
							ObjectTarget = null;
							return;
						}
					}
				}
				else if (num3 <= DistanceMoveTo)
				{
					if (aiState == 0)
					{
						Attack();
						PositionMove = base.transform.position;
						targetDirection = PositionTarget - base.transform.position;
					}
					if (aiState == 1)
					{
						PositionMove = PositionTarget;
						targetDirection = PositionTarget - base.transform.position;
					}
					if (aiState == 2)
					{
						PositionMove = positionMoveTmp;
						targetDirection = navAgent.desiredVelocity.normalized;
					}
				}
				else
				{
					ObjectTarget = null;
					aiState = 0;
				}
				if ((bool)component && !component.IsAlive)
				{
					ObjectTarget = null;
					aiState = 0;
				}
				SetDestination(PositionMove);
			}
			else
			{
				isShooting = false;
				if (Fighting)
				{
					ObjectTarget = findTarget();
				}
				if (ObjectTarget != null)
				{
					aiState = 0;
					return;
				}
				if (aiState == 0)
				{
					aiState = 1;
					aiTime = Random.Range(1f, IdleTime);
					PositionTarget = getRandomGo();
					PositionMove = PositionTarget;
					SetDestination(PositionMove);
				}
				if (aiTime <= 0f)
				{
					aiState = 0;
				}
				else
				{
					aiTime -= 1f / AIUpdateRate;
				}
				targetDirection = navAgent.desiredVelocity.normalized;
			}
			if (!LockTarget)
			{
				findTarget();
			}
		}
		attacking();
		Quaternion b2 = base.transform.rotation;
		if (targetDirection != Vector3.zero)
		{
			b2 = Quaternion.LookRotation(targetDirection);
			b2.x = 0f;
			b2.z = 0f;
			base.transform.rotation = Quaternion.Lerp(base.transform.rotation, b2, TurnSpeed * Time.deltaTime);
		}
		if ((bool)navAgent)
		{
			UnityEngine.Debug.DrawLine(base.transform.position, PositionMove, Color.green);
			navAgent.speed = character.GetCurrentMoveSpeed();
		}
	}

	private void SetDestination(Vector3 pos)
	{
		if ((bool)navAgent)
		{
			navAgent.SetDestination(pos);
		}
	}

	private void UNetVersion()
	{
	}

	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool result = default(bool);
		return result;
	}

	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
	}
}
